#include "scene.h"
#include <GL/gl.h>
#include <GL/glu.h>
#include <QDebug>
#include <QKeyEvent>

Scene::Scene() : QGraphicsScene(), terrain_(sceneLength_), cameraPosX_(0), cameraPosY_(0)
{
//    player_.setPosition( -(rand() % (sceneLength_/2)) );
}

void Scene::drawBackground(QPainter* painter, const QRectF& rect)
{
    glClearColor(.0, .1, .1, 1.0);
    glClear(GL_COLOR_BUFFER_BIT);
}

void Scene::drawForeground(QPainter* painter, const QRectF& rect)
{
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(-sceneLength_/2.0, sceneLength_/2.0, -sceneHeight_/2.0, sceneHeight_/2.0,
            -1.0, 1.0);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glClear(GL_COLOR_BUFFER_BIT);

    double scaleX = (double)sceneLength_ / rect.width();
    double scaleY = (double)sceneHeight_ / rect.height();

    qDebug() << "Scale: " << scaleX << " " << scaleY;

    glScaled(scaleX, scaleY, 0.0);

    terrain_.render();
    player_.render();
}

void Scene::keyPressEvent(QKeyEvent *event)
{
    switch(event->key()) {
    case Qt::Key_Left:
        {
        }
        break;
    case Qt::Key_Right:
        {
        }
        break;
    case Qt::Key_Plus:
        {
        }
        break;
    case Qt::Key_Minus:
        {
        }
        break;
    default:
        break;
    }
}
